using UnityEngine;

public class PlatformBalance : MonoBehaviour
{
	public float angleLimit = 15f;

	public bool autoMovement;

	public float autoMovementSpeed = 10f;

	public float autoMovementStartDelay;

	private int contacts;

	private bool activated;

	private float currentAngle;

	private Quaternion inititalRotation;

	private bool isActive;

	private int wait = 10;

	private bool init = true;

	private float direction = 1f;

	private float startTime;

	private bool autoRunning;

	private float myTime = -9999f;

	private void Awake()
	{
		LevelController.LevelReadyEvent += OnLevelReady;
		EventManager.WorldResetEvent += OnWorldReset;
		EventManager.GameFreezeEvent += OnGameFreeze;
		EventManager.GameUnfreezeEvent += OnGameUnfreeze;
	}

	private void OnDestroy()
	{
		LevelController.LevelReadyEvent -= OnLevelReady;
		EventManager.WorldResetEvent -= OnWorldReset;
		EventManager.GameFreezeEvent -= OnGameFreeze;
		EventManager.GameUnfreezeEvent -= OnGameUnfreeze;
	}

	private void OnLevelReady(Transform levelTransform)
	{
		if (base.transform.root == levelTransform)
		{
			isActive = true;
		}
	}

	private void OnWorldReset()
	{
		if (!base.GetComponent<Rigidbody>().isKinematic)
		{
			base.GetComponent<Rigidbody>().velocity = Vector3.zero;
			base.GetComponent<Rigidbody>().angularVelocity = Vector3.zero;
			base.GetComponent<Rigidbody>().isKinematic = true;
		}
		if (base.GetComponent<HingeJoint>() != null)
		{
			Object.DestroyImmediate(base.GetComponent<HingeJoint>());
		}
		base.transform.rotation = inititalRotation;
		direction = 1f;
		autoRunning = false;
		contacts = 0;
		wait = 0;
		activated = false;
		init = true;
	}

	private void OnGameFreeze()
	{
		if (!autoMovement)
		{
			base.GetComponent<Rigidbody>().isKinematic = true;
		}
	}

	private void OnGameUnfreeze()
	{
		if (!autoMovement)
		{
			base.GetComponent<Rigidbody>().isKinematic = false;
		}
	}

	private void OnCollisionEnter()
	{
		if (!autoMovement)
		{
			base.GetComponent<HingeJoint>().useMotor = false;
			contacts++;
			activated = true;
		}
	}

	private void OnCollisionExit()
	{
		if (!autoMovement)
		{
			contacts--;
		}
	}

	private void FixedUpdate()
	{
		if (Time.fixedDeltaTime < 0.001f)
		{
			return;
		}
		if (myTime == -9999f)
		{
			myTime = 0f - Time.fixedDeltaTime;
		}
		myTime += Time.fixedDeltaTime;
		if (!isActive)
		{
			return;
		}
		if (init)
		{
			Initialize();
			init = false;
		}
		if (init)
		{
			return;
		}
		if (autoMovement)
		{
			if (!autoRunning && myTime - startTime >= autoMovementStartDelay)
			{
				autoRunning = true;
			}
			if (autoRunning)
			{
				float num = Time.fixedDeltaTime * autoMovementSpeed * direction;
				currentAngle += num;
				if (currentAngle > angleLimit && direction > 0f)
				{
					direction *= -1f;
				}
				if (currentAngle < 0f - angleLimit && direction < 0f)
				{
					direction *= -1f;
				}
				base.GetComponent<Rigidbody>().MoveRotation(Quaternion.Euler(base.transform.TransformDirection(currentAngle * Vector3.right)) * inititalRotation);
			}
			return;
		}
		if (contacts == 0 && activated)
		{
			wait = 0;
			activated = false;
		}
		if (wait < 50000)
		{
			wait++;
		}
		if (contacts == 0 && wait >= 10)
		{
			if (Mathf.Abs(base.GetComponent<HingeJoint>().angle) > 0.5f)
			{
				base.GetComponent<HingeJoint>().useMotor = true;
				JointMotor motor = base.GetComponent<HingeJoint>().motor;
				motor.targetVelocity = Mathf.Abs(base.GetComponent<HingeJoint>().angle) * Mathf.Abs(base.GetComponent<HingeJoint>().angle) * Mathf.Sign(base.GetComponent<HingeJoint>().angle) * -1f;
				motor.force = Mathf.Clamp(Mathf.Abs(base.GetComponent<HingeJoint>().angle) * 0.16f, 0f, 2f);
				base.GetComponent<HingeJoint>().motor = motor;
			}
			else
			{
				base.GetComponent<HingeJoint>().useMotor = false;
				JointMotor motor2 = base.GetComponent<HingeJoint>().motor;
				motor2.targetVelocity = 0f;
				motor2.force = 0f;
				base.GetComponent<HingeJoint>().motor = motor2;
			}
		}
	}

	private void Initialize()
	{
		inititalRotation = base.transform.rotation;
		if (autoMovement)
		{
			currentAngle = 0f;
			startTime = myTime;
			base.GetComponent<Rigidbody>().isKinematic = true;
			return;
		}
		base.GetComponent<Rigidbody>().isKinematic = false;
		HingeJoint hingeJoint = base.gameObject.AddComponent<HingeJoint>();
		hingeJoint.anchor = Vector3.zero;
		hingeJoint.useLimits = true;
		JointLimits limits = hingeJoint.limits;
		limits.min = 0f - angleLimit;
		limits.max = angleLimit;
		hingeJoint.limits = limits;
	}
}
